package com.xqiapp.service.impl;

import com.xqiapp.jsp.mybatis.IBaseDao;
import com.xqiapp.entity.User;
import com.xqiapp.service.LoginService;
import com.xqiapp.bean.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rwrwd7 on 2017/9/6.
 *
 * 示例
 *
 */
@Service
public class LoginServiceImpl implements LoginService {

    static {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~init");
    }

    protected static final String MAPPING_SX = "com.xqiapp.mappers.UserMapper.";

    @Autowired
    private IBaseDao userMapper;

    @Override
    public User login(String username, String password) {
        Map map = new HashMap();
        map.put("username","admin");

        List<String> list = userMapper.find(MAPPING_SX+"findByUsername",map);
        String id = list.get(0);
        return new User(id,"admin");
    }


    public Page findPageByDescription(String description){

        Map param = new HashMap<>();
        param.put("description",description);
        Page list = userMapper.page(MAPPING_SX+"findPageByDescription",param,3,10);

        return list;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int addUser(User user) {
        int count = userMapper.insert(MAPPING_SX+"addUser",user);
        if(count > 0 ){
            //throw new RuntimeException("errors");
            //return count;
        }
        return 0;
    }


}
